package cn.yeziji.forum.converter;

import cn.yeziji.forum.dto.details.ForumDetailDTO;
import cn.yeziji.forum.entity.ForumDetailsEntity;
import cn.yeziji.forum.vo.details.DetailSpecialVO;
import cn.yeziji.forum.vo.details.DetailVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 文章转换器
 *
 * @author gzkemays
 * @since 2021/12/22 16:58
 */
@Mapper
public interface DetailsConverter {
  DetailsConverter INSTANCE = Mappers.getMapper(DetailsConverter.class);

  /**
   * dto 传输数据转化为实体类
   *
   * @param forumDetailDTO dto传输类
   * @return 实体类
   */
  /*  @Mapping(
  target = "content",
  expression = "java(cn.yeziji.forum.utils.GzipUtils.uncompress(forumDetailDTO.getContent()))")*/
  ForumDetailsEntity dtoConvertToEntity(ForumDetailDTO forumDetailDTO);

  /**
   * 实体类转化为前端渲染文章对象
   *
   * @param entity 文章实体类
   * @return 前端渲染对象
   */
  @Mapping(
      target = "publishTime",
      expression = "java(cn.yeziji.forum.vo.details.DetailVO.dateFormat(entity))")
  DetailVO entityConvertToDetail(ForumDetailsEntity entity);
  /**
   * 实体类转化为前端渲染文章对象列表
   *
   * @param entities 文章实体列表
   * @return 前端渲染对象列表
   */
  List<DetailVO> entitiesConvertToDetails(List<ForumDetailsEntity> entities);

  /**
   * map 转 detailVo
   *
   * @param map map 对象
   * @return detail vo
   */
  default DetailVO mapConvertToDetail(Map<String, Object> map) {
    if (map.isEmpty()) {
      return null;
    }
    DetailVO detailVO = new DetailVO();
    detailVO.setId((Integer) map.get("id"));
    detailVO.setUserId((Integer) map.get("userId"));
    detailVO.setAuthor((String) map.get("author"));
    detailVO.setAvatar((String) map.get("avatar"));
    detailVO.setAuthorDesc((String) map.get("authorDesc"));
    detailVO.setTitle((String) map.get("title"));
    detailVO.setContent((String) map.get("content"));
    detailVO.setDesc((String) map.get("desc"));
    detailVO.setTags((String) map.get("tags"));
    detailVO.setGoodNumber((Integer) map.get("goodNumber"));
    detailVO.setCommentNumber((Integer) map.get("commentNumber"));
    detailVO.setCollectionNumber((Integer) map.get("collectionNumber"));
    detailVO.setAutoFormat((Boolean) map.get("autoFormat"));
    detailVO.setAveRate((String) map.get("aveRate"));
    detailVO.setTempSave((Boolean) map.get("tempSave"));
    detailVO.setToRate((BigDecimal) map.get("toRate"));
    detailVO.setToGood((Boolean) map.get("toGood"));
    detailVO.setToCollection((Boolean) map.get("toCollection"));
    detailVO.setToComment((Boolean) map.get("toComment"));
    detailVO.setPublishTime((String) map.get("publishTime"));
    detailVO.setToRecovery((Boolean) map.get("delete"));
    detailVO.setSerialNumber((Long) map.get("serialNumber"));
    detailVO.setView((Long) map.get("view"));
    detailVO.setSpecial((String) map.get("special"));
    return detailVO;
  }

  /**
   * entity 转文章专栏
   *
   * @param entity 对象列表
   * @return 文章专栏
   */
  DetailSpecialVO entityConvertToDetailSpecial(ForumDetailsEntity entity);

  /**
   * entities 转文章专栏列表
   *
   * @param entities 实体对象列表
   * @return 专栏列表
   */
  List<DetailSpecialVO> entitiesConvertToDetailSpecials(List<ForumDetailsEntity> entities);
}
